package com.gr3g.dev.learndraw;

import java.io.File;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.gr3g.dev.learndraw.model.store.Datas;
import com.gr3g.dev.learndraw.model.view.DataView;

/** @author Gregory Tardivel */
public final class GalleryActivity extends AbstractScrollActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int nbParLigne = 0;

        final File[] fImages = Datas.chargerImages(this);

        for (final File image : fImages) {
            final DataView iBitmap = new DataView(this);
            iBitmap.setBitmap(image.getPath());
            iBitmap.setPadding(10, 5, 10, 5);
            iBitmap.setOnClickListener(this.loadDessin);

            this.addViewToRow(iBitmap);

            nbParLigne++;
            if (nbParLigne > 4) {
                // 5 images par ligne de tableau
                nbParLigne = 0;
                if (!image.equals(fImages[fImages.length - 1])) {
                    this.addRow();
                }
            }
        }
    }

    private final View.OnClickListener loadDessin = new View.OnClickListener() {

        @Override
        public void onClick(final View pView) {
            final DataView view = (DataView) pView;
            GalleryActivity.this.donnees.setDessin(view.getBitmap());
            GalleryActivity.this.startActivity(GalleryActivity.this
                    .nextActivity());
            GalleryActivity.this.finish();
        }

    };

    @Override
    protected Intent nextActivity() {
        return new Intent(this, FreeDrawActivity.class);
    }

    @Override
    protected int getViewTitle() {
        return R.string.menu_images;
    }

}
